home *** CD-ROM | disk | FTP | other *** search
- /****************************** pmrandom.h ***************************
-
- Purpose: Implement a random-number generator package for this program.
-
- Provenance: Written and tested by Q. Chen and E. Fox, March 1991.
- Edited by S. Wartik, April 1991.
-
- Notes: It is assumed that the C data type "int" can store
- 32-bit quantities.
- **/
-
- #include "pmrandom.h"
-
- static int seed = DEFAULT_SEED; /* The seed of the random number generator. */
-
- /*************************************************************************
-
- setseed(int)
-
- Returns: int
-
- Purpose: Set the seed for the random number generator.
-
- Plan: Uses a formula suggested by Park and Miller. See above.
-
- Notes: None.
- **/
-
- void setseed( new_seed )
- int new_seed;
- {
- int low, high, test;
-
- if ( (new_seed < 1) || (new_seed > 2147483646) )
- new_seed = DEFAULT_SEED;
- high = new_seed / 127773; /* 127773 = 2147483647 div 16807 */
- low = new_seed % 127773;
- test = 16807 * low - 2836 * high; /* 2836 = 2147483647 mod 16807 */
- seed = ( test > 0 ) ? test : test + 2147483647;
- }
-
-
- /*************************************************************************
-
- pmrandom()
-
- Returns: void
-
- Purpose: Return the next random number in the sequence.
-
- Plan: Uses the formula:
-
- f() = ( 16807 * seed ) mod 2147483647.
-
- The value of "seed" must be within [1, ..., 2147483646].
-
- Notes: None.
- **/
-
- int pmrandom()
- {
- int tmp = seed;
-
- setseed(seed);
- return(tmp);
- }
-
- /***********************************************************************
-
- getseed()
-
- Returns: int
-
- Purpose: Get the current value of the seed.
-
- Notes: None.
- **/
-
- int getseed()
- {
- return (seed);
- }
-